/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.runtime.op;

import lucee.commons.lang.ExceptionUtil;
import lucee.runtime.PageContext;
import lucee.runtime.interpreter.VariableInterpreter;
import lucee.runtime.type.Collection;
import lucee.runtime.type.KeyImpl;
import lucee.runtime.type.QueryColumn;
import lucee.runtime.type.scope.Scope;
import lucee.runtime.util.VariableUtilImpl;

public final class Elvis {

	/**
	 * called by the Elvis operator from generated bytecode
	 * 
	 * @param pc
	 * @param scope
	 * @param varNames
	 * @return
	 */
	@Deprecated
	public static boolean operate(PageContext pc, double scope, Collection.Key[] varNames) {
		return _load(pc, scope, varNames, 0, null) != null;
	}

	/**
	 * called by the Elvis operator from generated bytecode
	 * 
	 * @param pc
	 * @param scope
	 * @param varNames
	 * @return
	 */
	@Deprecated
	public static boolean operate(PageContext pc, double scope, String[] varNames) {
		return _load(pc, scope, KeyImpl.toKeyArray(varNames), 0, null) != null;
	}

	/**
	 * called by the Elvis operator from the interpreter
	 * 
	 * @param pc
	 * @param varNames
	 * @return
	 */
	@Deprecated
	public static boolean operate(PageContext pc, String[] varNames) {
		int scope = VariableInterpreter.scopeString2Int(pc.ignoreScopes(), varNames[0]);
		return _load(pc, scope, KeyImpl.toKeyArray(varNames), scope == Scope.SCOPE_UNDEFINED ? 0 : 1, null) != null;
	}

	/**
	 * called by the Elvis operator from generated bytecode
	 * 
	 * @param pc
	 * @param scope
	 * @param varNames
	 * @return
	 */
	public static Object load(PageContext pc, double scope, Collection.Key[] varNames) {
		return _load(pc, scope, varNames, 0, null);
	}

	/**
	 * called by the Elvis operator from generated bytecode
	 * 
	 * @param pc
	 * @param scope
	 * @param varNames
	 * @return
	 */
	public static Object load(PageContext pc, double scope, String[] varNames) {
		return _load(pc, scope, KeyImpl.toKeyArray(varNames), 0, null);
	}

	/**
	 * called by the Elvis operator from the interpreter
	 * 
	 * @param pc
	 * @param varNames
	 * @return
	 */
	public static Object load(PageContext pc, String[] varNames) {
		int scope = VariableInterpreter.scopeString2Int(pc.ignoreScopes(), varNames[0]);
		return _load(pc, scope, KeyImpl.toKeyArray(varNames), scope == Scope.SCOPE_UNDEFINED ? 0 : 1, null);
	}

	private static Object _load(PageContext pc, double scope, Collection.Key[] varNames, int startIndex, Object defaultValue) {
		Object coll;
		try {
			coll = VariableInterpreter.scope(pc, (int) scope, false);

			VariableUtilImpl vu = ((VariableUtilImpl) pc.getVariableUtil());
			for (int i = startIndex; i < varNames.length; i++) {
				// last
				if (i + 1 == varNames.length) {
					coll = vu.get(pc, coll, varNames[i], defaultValue);
				}
				else {
					coll = vu.getCollection(pc, coll, varNames[i], defaultValue);
				}

				if (i + 1 == varNames.length && coll instanceof QueryColumn) {
					if (((QueryColumn) coll).get(pc) == defaultValue) return defaultValue;
					return coll;
				}
				if (coll == defaultValue) return coll;
			}
		}
		catch (Throwable t) {
			ExceptionUtil.rethrowIfNecessary(t);
			return defaultValue;
		}
		return coll;
	}
}